package com.lpc.android.abc.main.activity;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TextView;

import com.lpc.android.abc.R;
import com.lpc.android.abc.base.base.BaseActivity;
import com.lpc.android.abc.base.bean.HomeCampaign;
import com.lpc.android.abc.base.bean.Tab;
import com.lpc.android.abc.base.utils.ToastUtils;
import com.lpc.android.abc.base.widget.FragmentTabHost;
import com.lpc.android.abc.main.fragment.CartFragment;
import com.lpc.android.abc.main.fragment.CategoryFragment;
import com.lpc.android.abc.main.fragment.HomeFragment;
import com.lpc.android.abc.main.fragment.HotFragment;
import com.lpc.android.abc.main.fragment.MineFragment;
import com.lpc.android.abc.main.mvp.MainContract;
import com.lpc.android.abc.main.mvp.MainPresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by lpc on 2016/11/16.
 */

public class MainActivity extends BaseActivity<MainPresenter> implements MainContract.View {

    @BindView(android.R.id.tabhost)
    FragmentTabHost tabhost;

    private List<Tab> mTabs = new ArrayList<>(5);

    @Override
    protected int setActivityLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initData() {

        //不同打开方式引发的多次启动问题
        if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
            finish();
            return;
        }

        initTab();
    }

    private void initTab() {

        Tab tab_home = new Tab(HomeFragment.class, R.string.home, R.drawable.selector_icon_home);
        Tab tab_hot = new Tab(HotFragment.class, R.string.hot, R.drawable.selector_icon_hot);
        Tab tab_category = new Tab(CategoryFragment.class, R.string.catagory, R.drawable.selector_icon_category);
        Tab tab_cart = new Tab(CartFragment.class, R.string.cart, R.drawable.selector_icon_cart);
        Tab tab_mine = new Tab(MineFragment.class, R.string.mine, R.drawable.selector_icon_mine);

        mTabs.add(tab_home);
        mTabs.add(tab_hot);
        mTabs.add(tab_category);
        mTabs.add(tab_cart);
        mTabs.add(tab_mine);

        tabhost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

        for (Tab tab : mTabs) {

            TabHost.TabSpec tabSpec = tabhost.newTabSpec(getString(tab.getTitle()));

            tabSpec.setIndicator(buildIndicator(tab));

            tabhost.addTab(tabSpec, tab.getFragment(), null);

        }

//        tabhost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
//            @Override
//            public void onTabChanged(String tabId) {
//
//                if(tabId==getString(R.string.cart)){
//
//                    refData();
//                }
//
//            }
//        });

        tabhost.getTabWidget().setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);
        tabhost.setCurrentTab(0);
    }

    private View buildIndicator(Tab tab) {

        View view = View.inflate(this, R.layout.tab_indicator, null);
        ImageView img = ButterKnife.findById(view, R.id.icon_tab);
        TextView text = ButterKnife.findById(view, R.id.txt_indicator);

        img.setBackgroundResource(tab.getIcon());
        text.setText(tab.getTitle());

        return view;
    }

    public void click() {

        mPresenter = new MainPresenter(this);
        mPresenter.getHomeInfo();
    }

    @Override
    public void homeInfoSuccess(List<HomeCampaign> lists) {

    }

    @Override
    public void homeInfoFail(String fail) {
        ToastUtils.showShortToast(fail);
    }

}
